package cn.dotiedu.redis;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.*;

import java.util.*;
import java.util.function.Predicate;

/***
 * @author hunter.d
 * @qq 657270652
 * @wx haitao-duan
 * @date 2020/11/17
 **/
public class JedisDemo {
    JedisCluster jedis2 = null;
    Jedis jedis = null;

    @Before
    public void init() {
        // 方式1，创建连接
        // Jedis jedis2 = new Jedis("doitedu03", 6379);

        // 方式2，创建连接池
        JedisPool pool = new JedisPool("doitedu03", 6379);
        jedis = pool.getResource();

        //HostAndPort hp = new HostAndPort("doitedu01", 6379);
        //JedisCluster jedisCluster = new JedisCluster(hp);
        //jedisCluster.set("aaa", "valuea");

    }

    @Test
    public void testBitSet() {


        BitSet bitSet1 = BitSet.valueOf(jedis.get("bitb".getBytes()));
        int cardinality = bitSet1.cardinality();
        System.out.println(cardinality);

    }


    @Test
    public void testKeys() {

        //Set<String> keys = jedis.keys("name.*");
        //System.out.println(keys);

        System.out.println(jedis.pfcount("hunan"));
    }

    @Test
    public void testClusterKeys() {

        /*Map<String, JedisPool> clusterNodes = jedis.getClusterNodes();
        HashSet<String> allKeys = new HashSet<>();

        clusterNodes.values().forEach((pool)->{
            Jedis resource = pool.getResource();
            Set<String> keys = resource.keys("*");
            allKeys.addAll(keys);
            resource.close();
        });*/

    }


    @Test
    public void testGet() {


        String pong = jedis.ping();
        System.out.println(pong);

        jedis.set("key1", "value1");
        String key1 = jedis.get("key1");

        long s = jedis.pfcount("hunan:changsha");
        System.out.println(s);
    }


    @Test
    public void testPipeLine() {
        Pipeline pipelined = jedis.pipelined();
        pipelined.set("a", "aaa");
        pipelined.set("b", "bbb");
        pipelined.set("c", "ccc");
        pipelined.set("d", "ddd");
        pipelined.get("d");
        List<Object> objects = pipelined.syncAndReturnAll();
        System.out.println(objects);
    }

    @Test
    public void testPipeLine2() {
        Pipeline pipelined = jedis.pipelined();
        pipelined.multi();
        pipelined.set("a", "aaa");
        pipelined.set("b", "bbb");
        pipelined.set("c", "ccc");
        pipelined.set("d", "ddd");
        Response<String> d = pipelined.get("d");
        pipelined.exec();
        //List<Object> objects = pipelined.syncAndReturnAll();
        pipelined.close();
        System.out.println(d.get());
    }

    @Test
    public void testMulti() {
        Transaction multi = jedis.multi();

        multi.set("a", "aaa");
        multi.set("b", "bbb");
        multi.set("c", "ccc");
        multi.set("d", "ddd");
        Response<String> d = multi.get("d");
        multi.exec();
        System.out.println(d.get());
        multi.close();
    }


}
